/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	GL Render target manager :
-----------------------------------------------------------------------------*/

class EGLRTManager : public IRTManager {
	public:
							EGLRTManager			( EGLDriver *driver );
		virtual				~EGLRTManager			( void );
		
		virtual IPxTexture	AllocRenderTarget		( ERSFormat_t format, int width, int height );
		virtual void		ClearFrameBuffer		( uint target, const EColor &color, float depth=1, uint stencil=0 );
		
		virtual void		SetFrameBuffer			( IPxTexture ds, IPxTexture c0, IPxTexture c1=0, IPxTexture c2=0, IPxTexture c3=0 );
		virtual void		ResetFrameBuffer		( void );
		
		virtual void		CopyColorFrameData		( IPxTexture dst, IPxTexture src, bool filter );
		virtual void		CopyDepthFrameData		( IPxTexture dst, IPxTexture src );
		virtual void		CopyColorToBackBuffer	( IPxTexture src, bool left, bool right, bool filter );
		
		virtual void		SetStereoTarget			( bool left, bool right );

		virtual void		MakeScreenshot			( EString path, ERSImageFormat_t image_format );

	protected:
		void				SetDrawFBOInternal		( IPxTexture ds, IPxTexture c0, IPxTexture c1=0, IPxTexture c2=0, IPxTexture c3=0 );
		void				SetReadFBOInternal		( IPxTexture ds, IPxTexture c0, IPxTexture c1=0, IPxTexture c2=0, IPxTexture c3=0 );
		GLuint				AllocFBOInternal		( GLuint ds, GLuint c0, GLuint c1, GLuint c2, GLuint c3 );
		void				FreeFBOs				( void );
										
	protected:
		GLenum	stereo_target;
	
		struct FBO {
				GLuint	fbo_obj;
				GLuint	depth_stencil;
				GLuint	color0;
				GLuint	color1;
				GLuint	color2;
				GLuint	color3;
			};
		
		std::vector<IPxTexture>	rt_pool;
		std::vector<FBO>			fbo_pool;
		
		struct {
			IPxTexture	ds;
			IPxTexture	c0;
			IPxTexture	c1;
			IPxTexture	c2;
			IPxTexture	c3;
		} current_frame_buffer;
		
		EGLDriver	*driver;
	};